#include "rightform.h"
#include "qdebug.h"
#include "qmediaplaylist.h"
#include "rightformitem.h"
#include "ui_rightform.h"
#include <QFileDialog>

rightForm::rightForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::rightForm)
    ,isLike(false)
{
    ui->setupUi(this);

    connect(ui->pushButton, &QPushButton::clicked, this, [=](){
        emit play(Pagetype);
    });
    connect(ui->listWidget,&QListWidget::doubleClicked,this,[=](const QModelIndex &index){
        emit playMusicByIndex(this,index.row());
    });
}

rightForm::~rightForm()
{
    delete ui;
}

void rightForm::setPageText(QString text)
{
    ui->label->setText(text);
    //测试添加
    // rightFormitem* box = new rightFormitem(this);
    // QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
    // item->setSizeHint(QSize(box->width(),box->height()));
    // ui->listWidget->setItemWidget(item, box);
}

void rightForm::setPageType(enum Page Pagetype)
{
    this->Pagetype = Pagetype;
    // qDebug() << "Page type set to:" << static_cast<int>(Pagetype);
}

void rightForm::addToRightPage(listForm musiclist) {
    musicPage.clear();
    for(auto music = musiclist.begin(); music != musiclist.end(); music++) {
        switch(Pagetype) {
        case Page::likePage:
            if(music->getisLikeitem()) {
                musicPage.push_back(music->getitemId());
            }
            break;
        case Page::loadPage:
            musicPage.push_back(music->getitemId());
            break;
        case Page::historyPage:
            if(music->getisHistoryitem()) {
                musicPage.push_back(music->getitemId());
            }
            break;
        default:
            qDebug() << "Unknown page type";
            break;
        }
    }
}

void rightForm::showItemonPage(listForm musiclist)
{
    ui->listWidget->clear();
    addToRightPage(musiclist);
    for(const auto &pageid : musicPage)
    {
        auto it = musiclist.getMusicId(pageid);
        if(it == musiclist.end())
            continue;

        //添加内容到widget
        rightFormitem* box = new rightFormitem(it->getisLikeitem(), this);

        box->setNameitem(it->getNameitem());
        box->setAuthoritem(it->getAuthoritem());
        box->setAlbumitem(it->getAlbumitem());

        QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
        item->setSizeHint(QSize(box->width(),box->height()));
        ui->listWidget->setItemWidget(item, box);

        connect(box, &rightFormitem::setisLike,this,[pageid, this](bool isLike){
            emit upDatePage(isLike, pageid);
        });
    }
    ui->listWidget->repaint();
}

void rightForm::addMusicToPlayList(listForm list, QMediaPlaylist *playlist)
{
    for(auto it = list.begin(); it != list.end(); it++)
    {
        switch (Pagetype) {
        case Page::loadPage:
        {
            playlist->addMedia(it->getUrlitem());
            break;
        }
        case Page::likePage:
        {
            if(it->getisLikeitem())
            {
                playlist->addMedia(it->getUrlitem());
            }
            break;
        }
        case Page::historyPage:
        {
            if(it->getisHistoryitem())
            {
                playlist->addMedia(it->getUrlitem());
            }
            break;
        }

        default:
            break;
        }
    }
}

QString rightForm::getMusicId(int index)
{
    if (index >= 0 && index < musicPage.size()) {
        return musicPage[index];
    }
    return "";
}

void rightForm::on_pushButton_2_clicked()
{
    QFileDialog filedialog(this);
    QDir dir(QDir::currentPath());
    dir.cdUp();
    dir.cdUp();
    QString path = dir.path();
    path += "/music/downloadmusic";

    //创建文件选择对话框
    filedialog.setWindowTitle("选择本地音乐文件");
    filedialog.setDirectory(path);

    //文件过滤
    QStringList list;
    list << "audio/mpeg" << "audio/ogg" << "audio/flac";
    filedialog.setMimeTypeFilters(list);
    //多选模式
    filedialog.setFileMode(QFileDialog::ExistingFiles);
    if(filedialog.exec() == QDialog::Accepted)
    {
        QList<QUrl> musicurls = filedialog.selectedUrls();
        //获取音乐源+到列表
        musiclist.getMusicByUrl(musicurls);
        emit musicFilesSelected(musiclist);
        showItemonPage(musiclist);
        qDebug() << musicurls;
    }
}


void rightForm::setMusicListPage(Page Pagetype)
{
    this->Pagetype = Pagetype;
}
